package com.ehotting.mqbd.core.web;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.mqbd.api.MqDataStateConstants;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.enums.TopicEnum;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MqWebHelper {

    public static R handleResult(FutureTask<MQDataAble> futureTask, MQDataAble data){
        R result = new R();
        if (data.getTopic() == TopicEnum.BLOCK) {
            result.setCode(MqDataStateConstants.SEND_SUCCESS.equals(data.getState()) || MqDataStateConstants.EXISTS_IN_QUEUE.equals(data.getState())?R.SUCCESS:R.FAIL);
            result.setMsg(data.getMsg());
        }else{
            result.setCode(MqDataStateConstants.SEND_SUCCESS.equals(data.getState())?R.SUCCESS:R.FAIL);
            result.setMsg(data.getMsg());
            if(futureTask!=null){
                try {
                    MQDataAble response =  futureTask.get();
                    if(response!=null){
                        result.setData(response.getResponse());
                    }else{
                        //result.setData("响应结果为空");
                    }
                } catch(InterruptedException e){
                    result.setCode(R.FAIL);
                    result.setMsg("发送失败");
                    e.printStackTrace();
                } catch(ExecutionException e){
                    result.setCode(R.FAIL);
                    result.setMsg("发送失败");
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
